package com.mouse.remind;

import java.lang.reflect.Field;
import java.util.Calendar;


import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;


/**
 * 修复bug的dialog类，提供与DatePickerDialog一样的方法
 * 
 * @version 1.0
 *
 */
public class CustomDatePickerDialog extends DatePickerDialog {
	/*
	  public CustomDatePickerDialog(Context context, OnDateSetListener callBack,
			int year, int monthOfYear, int dayOfMonth) {
		super(context, null, year, monthOfYear, dayOfMonth);
		initializePicker(callBack);
	}

	//I use a Calendar object to initialize it, but you can revert to Y,M,D easily
	  public CustomDatePickerDialog(Calendar dateToShow, Context context, OnDateSetListener callBack) {
	    super(context, null, dateToShow.get(Calendar.YEAR), dateToShow.get(Calendar.MONTH), dateToShow.get(Calendar.DAY_OF_MONTH));
	    initializePicker(callBack);
	  }

	  public CustomDatePickerDialog(Calendar dateToShow, Context context, int theme,
	    OnDateSetListener callBack) {
	    super(context, theme, null, dateToShow.get(Calendar.YEAR), dateToShow.get(Calendar.MONTH), dateToShow.get(Calendar.DAY_OF_MONTH));
	    initializePicker(callBack);
	  }
	  */
	int version = Integer.valueOf(android.os.Build.VERSION.SDK);
	public CustomDatePickerDialog(Context context,
            OnDateSetListener callBack,int year, int monthOfYear, int day) {
        super(context, callBack, year, monthOfYear, day);
        this.setTitle((monthOfYear + 1) + "月"  + day + "日");
        initializePicker(callBack);
    }
	

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        this.setTitle((month + 1) + "月"+ (day) + "日" );
    }
    @Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		 DatePicker dp = findDatePicker((ViewGroup) this.getWindow().getDecorView());
	        if (dp != null) {
	        	Class c=dp.getClass();
	        	Field f;
				try {
						if(version>13){
							f = c.getDeclaredField("mYearSpinner");
							f.setAccessible(true );  
							LinearLayout l= (LinearLayout)f.get(dp);   
							l.setVisibility(View.GONE);
						}else{
							f = c.getDeclaredField("mYearPicker");
							f.setAccessible(true );  
							LinearLayout l= (LinearLayout)f.get(dp);   
							l.setVisibility(View.GONE);
						}
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}  
	        	
	        } 
	}
    private DatePicker findDatePicker(ViewGroup group) {
        if (group != null) {
            for (int i = 0, j = group.getChildCount(); i < j; i++) {
                View child = group.getChildAt(i);
                if (child instanceof DatePicker) {
                    return (DatePicker) child;
                } else if (child instanceof ViewGroup) {
                    DatePicker result = findDatePicker((ViewGroup) child);
                    if (result != null)
                        return result;
                } 
            }
        }
        return null;

    } 
	private void initializePicker(final OnDateSetListener callback) {
	    try {
	      //If you're only using Honeycomb+ then you can just call getDatePicker() instead of using reflection
	      Field pickerField = DatePickerDialog.class.getDeclaredField("mDatePicker");
	      pickerField.setAccessible(true);
	      final DatePicker picker = (DatePicker) pickerField.get(this);
	      this.setCancelable(true);
	      this.setButton(DialogInterface.BUTTON_NEGATIVE, getContext().getText(android.R.string.cancel), (OnClickListener) null);
	      this.setButton(DialogInterface.BUTTON_POSITIVE, getContext().getText(android.R.string.ok),
	          new DialogInterface.OnClickListener() {
	              @Override
	              public void onClick(DialogInterface dialog, int which) {
	                picker.clearFocus(); //Focus must be cleared so the value change listener is called
	                callback.onDateSet(picker, picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
	              }
	          });
	    } catch (Exception e) { /* Reflection probably failed*/ }
   }
}